package picobot.rule;

import picobot.interfaces.core.IRule;

/**
 * a class to represent a rule
 */
public class Rule implements IRule {

	private int requiredState, targetState;
	private String destination;
	private String[] surrounding = new String[4];

	/**
	 * the constructor of this class using it's fields
	 * 
	 * @param requiredState
	 *            is the required state of the rule
	 * @param surrounding
	 *            is the required surrounding
	 * @param destination
	 *            is the destination of the rule, i.e. the direction the picobot
	 *            will go
	 * @param targetState
	 *            is the target state, i.e. the state the picobot will take
	 */
	public Rule(int requiredState, String[] surrounding, String destination,
			int targetState) {
		super();
		this.requiredState = requiredState;
		this.surrounding = surrounding;
		this.destination = destination;
		this.targetState = targetState;

	}

	@Override
	public String getConditionAt(String place) {
		String condition = null;

		if (place.equals("NORTH")) {
			condition = surrounding[0];
		} else {
			if (place.equals("EAST")) {
				condition = surrounding[1];
			} else {
				if (place.equals("WEST")) {
					condition = surrounding[2];
				} else {
					if (place.equals("SOUTH")) {
						condition = surrounding[3];
					} else {
						System.err.println("Wrong input!");
					}
				}
			}
		}
		return condition;
	}

	@Override
	public String getDestination() {
		return destination;
	}

	@Override
	public int getRequiredState() {
		return requiredState;
	}

	@Override
	public int getTargetState() {
		return targetState;
	}

	@Override
	public String toString() {
		String ruleString = "";

		try {
			// the required state
			ruleString += requiredState + " ";

			// north
			if (surrounding[0].equals("WALL")) {
				ruleString += "N";
			} else {
				if (surrounding[0].equals("FREE")) {
					ruleString += "x";
				} else {
					if (surrounding[0].equals("ANY")) {
						ruleString += "*";
					}
				}
			}
			// east
			if (surrounding[1].equals("WALL")) {
				ruleString += "E";
			} else {
				if (surrounding[1].equals("FREE")) {
					ruleString += "x";
				} else {
					if (surrounding[1].equals("ANY")) {
						ruleString += "*";
					}
				}
			}
			// west
			if (surrounding[2].equals("WALL")) {
				ruleString += "W";
			} else {
				if (surrounding[2].equals("FREE")) {
					ruleString += "x";
				} else {
					if (surrounding[2].equals("ANY")) {
						ruleString += "*";
					}
				}
			}
			// south
			if (surrounding[3].equals("WALL")) {
				ruleString += "S";
			} else {
				if (surrounding[3].equals("FREE")) {
					ruleString += "x";
				} else {
					if (surrounding[3].equals("ANY")) {
						ruleString += "*";
					}
				}
			}

			// operation arrow
			ruleString += " -> ";

			// destination
			if (destination.equals("NORTH")) {
				ruleString += "N";
			} else {
				if (destination.equals("EAST")) {
					ruleString += "E";
				} else {
					if (destination.equals("WEST")) {
						ruleString += "W";
					} else {
						if (destination.equals("SOUTH")) {
							ruleString += "S";
						} else {
							if (destination.equals("STAY")) {
								ruleString += "X";
							}
						}
					}
				}
			}

			// target state
			ruleString += " " + targetState;

		} catch (Exception e) {
			System.err.println("Your rule has the wrong format:"
					+ e.getMessage());
		}

		return ruleString;
	}

}
